function [largestVelConComp, filteredCurve] = removeArtifacts (threshold, windowedImg, nCol, arraySize, y, counter)

filteredCurve = im2bw(windowedImg, threshold);

largestVelConComp = bwareafilt(filteredCurve, 1, 'largest');

for i = 1:nCol
    
    y(i, 1) = sum(largestVelConComp(:, i) == 1);
    
end

y = y / arraySize(1);


%Displays a warning if function is forced to exit the recursive loop
if counter == 400
    
    warning('Artifact removal may have failed. View results to ensure acuracy')
    
end


if any(y(y > 0.65)) == true && counter < 400 %function can only be called recursively 400 times at most
    
    counter = counter + 1; %Ensures that the function does not get caught in a loop infinitely
    
    threshold = threshold + 0.01;
    [largestVelConComp, filteredCurve] = removeArtifacts (threshold, windowedImg, nCol, arraySize, y, counter);
    
else
    
    return
    
end

end
